package com.zmq.pic.util;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class CreateBase {

	/**
	 * @Title createDAO
	 * @author 陈希峰 chenxf10@126.com
	 * @Description 生成DAO层
	 * @param entityDir
	 *            entity所在的目录
	 * @param basePackagePath
	 *            entity上层的package路径
	 * @param dateStr
	 *            时间字符串
	 * @return
	 * @throws IOException
	 * @return boolean
	 * @throws
	 */
	public boolean createDAO(File entityDir, String basePackagePath,
			String dateStr) throws IOException {
		File daoDir = new File(entityDir.getParent(), "dao");
		daoDir.mkdir();
		String[] fileNames = entityDir.list();
		File dao = null;
		String entityClassName;
		for (String s : fileNames) {
			entityClassName = s.split("\\.")[0];
			dao = new File(daoDir, s.replaceAll("\\.", "DAO."));
			StringBuffer sbDao = new StringBuffer();
			sbDao.append("package ").append(basePackagePath).append(".dao;\n");
			sbDao.append("\n");
			sbDao.append("import org.springframework.stereotype.Repository;")
					.append("\n");
			sbDao.append("import ").append(basePackagePath)
					.append(".common.HibernateDao;").append("\n");
			sbDao.append("import ").append(basePackagePath).append(".entity.")
					.append(entityClassName).append(";\n");
			sbDao.append("\n");
			sbDao.append("/**").append("\n");
			sbDao.append(" * @author zmq ").append("\n");
			sbDao.append(" * @createTime ").append(dateStr).append("\n");
			sbDao.append(" */").append("\n");
			sbDao.append("@Repository").append("\n");
			sbDao.append("public class ").append(entityClassName)
					.append("DAO extends HibernateDao<")
					.append(entityClassName).append("> {\n");
			sbDao.append("}");
			writeFile(dao, sbDao.toString());
		}
		return true;
	}

	/**
	 * @Title createServices
	 * @author 陈希峰 chenxf10@126.com
	 * @Description 生成Service层
	 * @param entityDir
	 *            entity所在的目录
	 * @param basePackagePath
	 *            entity上层的package路径
	 * @param dateStr
	 *            时间字符串
	 * @return
	 * @throws IOException
	 * @return boolean
	 * @throws
	 */
	public boolean createServices(File entityDir, String basePackagePath,
			String dateStr) throws IOException {
		File serviceDir = new File(entityDir.getParent(), "service");
		serviceDir.mkdir();
		File serviceImplDir = new File(serviceDir, "impl");
		serviceImplDir.mkdir();
		String[] fileNames = entityDir.list();
		File service, serviceImpl;
		String entityClassName;
		StringBuffer sbService, sbServiceImpl;
		for (String s : fileNames) {
			entityClassName = s.split("\\.")[0];
			service = new File(serviceDir, "I"
					+ s.replaceAll("\\.", "Service."));
			sbService = new StringBuffer();
			sbService.append("package ").append(basePackagePath)
					.append(".service;\n");
			sbService.append("\n");
			sbService.append("import ").append(basePackagePath)
					.append(".entity.").append(entityClassName).append(";\n");
			sbService.append("\n");
//			sbService.append("import ").append(basePackagePath)
//					.append(".common.ICommonServices;\n");
//			sbService.append("\n");
			sbService.append("/**").append("\n");
			sbService.append(" * @author zmq ").append("\n");
			sbService.append(" * @createTime ").append(dateStr).append("\n");
			sbService.append(" */").append("\n");
			sbService.append("public interface I").append(entityClassName)
					.append("Service extends ICommonService<")
					.append(entityClassName).append("> {\n");
			sbService.append("}");
			writeFile(service, sbService.toString());

			sbServiceImpl = new StringBuffer();
			sbServiceImpl.append("package ").append(basePackagePath)
					.append(".service.impl;\n");
			sbServiceImpl.append("\n");
			sbServiceImpl
					.append("import org.springframework.beans.factory.annotation.Autowired;")
					.append("\n");
			sbServiceImpl.append(
					"import org.springframework.stereotype.Service;").append(
					"\n");
			sbServiceImpl.append("import ").append(basePackagePath)
					.append(".common.HibernateDao;").append("\n");
			sbServiceImpl.append("import ").append(basePackagePath)
					.append(".common.Page;").append("\n");
//			sbServiceImpl.append("import ").append(basePackagePath)
//					.append(".common.CommonServicesImpl;\n");
			sbServiceImpl.append("import ").append(basePackagePath)
					.append(".dao.").append(entityClassName).append("DAO;\n");
			sbServiceImpl.append("import ").append(basePackagePath)
					.append(".entity.").append(entityClassName).append(";\n");
			sbServiceImpl.append("import ").append(basePackagePath)
					.append(".service.I").append(entityClassName)
					.append("Service;\n");
			sbServiceImpl.append("\n");
			sbServiceImpl.append("/**").append("\n");
			sbServiceImpl.append(" * @author xifeng chenxf10@126.com").append(
					"\n");
			sbServiceImpl.append(" * @createTime ").append(dateStr)
					.append("\n");
			sbServiceImpl.append(" */").append("\n");
			sbServiceImpl.append("@Service").append("\n");
			sbServiceImpl.append("public class ").append(entityClassName)
					.append("ServiceImpl extends CommonServiceImpl<")
					.append(entityClassName).append("> implements I")
					.append(entityClassName).append("Service {\n");
			String daoName = entityClassName.toLowerCase().substring(0, 1)
					+ entityClassName.substring(1) + "DAO";
			sbServiceImpl.append("	@Autowired\n");
			sbServiceImpl.append("	private ").append(entityClassName)
					.append("DAO ").append(daoName).append(";\n");
			sbServiceImpl.append("\n");
			sbServiceImpl.append("	@Override\n");
			sbServiceImpl.append("	public HibernateDao<")
					.append(entityClassName).append("> getHibernateDao() { ")
					.append("\n");
			sbServiceImpl.append("		return ").append(daoName).append(";\n");
			sbServiceImpl.append("	}\n");
			sbServiceImpl.append("\n");
			sbServiceImpl.append("	@Override\n");
			sbServiceImpl.append("	public void findByExample(")
					.append(entityClassName).append(" ")
					.append(daoName.replace("DAO", "")).append(", Page<")
					.append(entityClassName).append("> page ) { ").append("\n");
			sbServiceImpl.append("\n");
			sbServiceImpl.append("	}\n");
			sbServiceImpl.append("\n");
			sbServiceImpl.append("}");

			serviceImpl = new File(serviceImplDir, s.replaceAll("\\.",
					"ServiceImpl."));
			writeFile(serviceImpl, sbServiceImpl.toString());
		}
		return true;
	}

	/**
	 * @Title writeFile
	 * @author 陈希峰 chenxf10@126.com
	 * @Description 生成文件
	 * @param file
	 *            文件
	 * @param fileContent
	 *            文件内容
	 * @throws IOException
	 * @return void
	 * @throws
	 */
	private void writeFile(File file, String fileContent) throws IOException {
		BufferedWriter bw = new BufferedWriter(new FileWriter(file));
		bw.write(fileContent);
		bw.flush();
		bw.close();
	}

	public static void main(String[] args) throws IOException {
		CreateBase base = new CreateBase();
		
		String entityFilesPath = "G:\\MyEclipseExtend\\Workspaces\\PIC_Online_1.0\\src\\com\\zmq\\pic\\entity";
		String basePackagePath = "com.zmq.pic";
		File entityFile = new File(entityFilesPath);
		SimpleDateFormat dateformat2 = new SimpleDateFormat(
				"yyyy-MM-dd HH:mm:ss ");
		String dateStr = dateformat2.format(new Date());
		if (entityFile.exists() && entityFile.isDirectory()) {
			base.createDAO(entityFile, basePackagePath, dateStr);
			base.createServices(entityFile, basePackagePath, dateStr);
		}
	}
}